package cn.gbase.jiangsu.data.transfer.util;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.log4j.Logger;

/**
 * 文件操作工具类
 * 
 * @author Administrator
 * 
 */
public class FileUtils {

	private static final Logger logger = Logger.getLogger(FileUtils.class);

	/**
	 * 创建一个新的文件
	 * 
	 * @param fileName
	 *            文件名称
	 * @param delete
	 *            是否删除旧文件
	 * @return 成功true,成功false
	 */
	public static boolean createFile(String fileName, boolean delete) {

		File file = new File(fileName);

		if (file.exists()) {

			if (!delete) {
				return true;
			}

			if (file.delete()) {
				try {
					if (file.createNewFile()) {
						return true;
					}
				} catch (IOException e) {
					logger.fatal(e);
				}
			}
		}

		return false;
	}

	/**
	 * 删除文件
	 * 
	 * @param path
	 *            文件路径
	 * @return 成功返回true,成功返回false
	 */
	public static boolean deleteFile(String path) {

		File file = new File(path);

		if (file.exists()) {
			if (!file.delete()) {
				return false;
			}
		}

		return true;
	}

	/**
	 * 创建文件夹
	 * 
	 * @param path
	 *            路径
	 * @return true 成功，false 失败
	 */
	public static boolean createFloader(String path) {

		File file = new File(path);

		if (file.exists()) {
			if (!file.isDirectory()) {
				if (!file.mkdirs()) {

					return false;

				}
			}
		}

		return true;
	}

	/**
	 * 移动文件
	 * 
	 * @param source
	 *            要移动的文件
	 * @param target
	 *            移动到的绝对路径
	 * @return
	 */
	public static boolean moveFile(File source, String target) {

		if (source == null || target == null || "".equals(target)) {
			return false;
		}

		int dot = target.lastIndexOf(File.separator);
		String floder = target.substring(0, dot);
		File fl = new File(floder);
		if (!fl.exists()) {
			fl.mkdirs();
		}

		File f = new File(target);

		if (!f.exists()) {
			return source.renameTo(f);
		}

		return false;
	}

	/**
	 * Read bytes from a File into a byte[].
	 * 
	 * @param file
	 *            The File to read.
	 * @return A byte[] containing the contents of the File.
	 * @throws IOException
	 *             Thrown if the File is too long to read or couldn't be read
	 *             fully.
	 */
	public static byte[] readBytesFromFile(File file) throws IOException {
		InputStream is = new FileInputStream(file);

		// Get the size of the file
		long length = file.length();

		// You cannot create an array using a long type.
		// It needs to be an int type.
		// Before converting to an int type, check
		// to ensure that file is not larger than Integer.MAX_VALUE.
		if (length > Integer.MAX_VALUE) {
			throw new IOException("Could not completely read file "
					+ file.getName() + " as it is too long (" + length
					+ " bytes, max supported " + Integer.MAX_VALUE + ")");
		}

		// Create the byte array to hold the data
		byte[] bytes = new byte[(int) length];

		// Read in the bytes
		int offset = 0;
		int numRead = 0;
		while (offset < bytes.length
				&& (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
			offset += numRead;
		}

		// Ensure all the bytes have been read in
		if (offset < bytes.length) {
			throw new IOException("Could not completely read file "
					+ file.getName());
		}

		// Close the input stream and return bytes
		is.close();
		return bytes;
	}

	/**
	 * Writes the specified byte[] to the specified File path.
	 * 
	 * @param theFile
	 *            File Object representing the path to write to.
	 * @param bytes
	 *            The byte[] of data to write to the File.
	 * @throws IOException
	 *             Thrown if there is problem creating or writing the File.
	 */
	public static void writeBytesToFile(File theFile, byte[] bytes)
			throws IOException {
		BufferedOutputStream bos = null;

		try {
			FileOutputStream fos = new FileOutputStream(theFile);
			bos = new BufferedOutputStream(fos);
			bos.write(bytes);
		} finally {
			if (bos != null) {
				try {
					// flush and close the BufferedOutputStream
					bos.flush();
					bos.close();
				} catch (Exception e) {
				}
			}
		}
	}
}
